package xyz.weizhi.rjwm.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.weizhi.rjwm.common.CustomException;
import xyz.weizhi.rjwm.common.baseContext;
import xyz.weizhi.rjwm.entity.*;
import xyz.weizhi.rjwm.mapper.OrdersDao;
import xyz.weizhi.rjwm.service.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Author milian
 * @Date 2021/9/28 0028 10:13
 */
@Service
public class OrdersServiceImp extends ServiceImpl<OrdersDao, Orders> implements OrdersService {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Override
    @Transactional
    public void submit(Orders orders) {
        Long userID = baseContext.getThreadLocal();
        //查询购物车信息
        LambdaQueryWrapper<ShoppingCart> sclqw = new LambdaQueryWrapper<>();
        sclqw.eq(ShoppingCart::getUserId,userID);
        List<ShoppingCart> shoppingCartsList = shoppingCartService.list(sclqw);
        if (shoppingCartsList==null||shoppingCartsList.size()==0){
            throw new CustomException("购物车为空不能删除");
        }
        //查询用户信息
        User userInfo = userService.getById(userID);
        //查询地址数据
        AddressBook addressBookInfo = addressBookService.getById(orders.getAddressBookId());
        if (addressBookInfo==null){
            new CustomException("用户地址信息有误，不能下单");
        }
        //获取订单号
        long orderId = IdWorker.getId();
        AtomicInteger amount = new AtomicInteger(0);
        List<OrderDetail> orderDetails = shoppingCartsList.stream().map(item -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setAmount(item.getAmount());
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());
        orders.setUserId(userID);
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(2);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(new BigDecimal(amount.get()));
        orders.setUserName(userInfo.getName());
        orders.setPhone(addressBookInfo.getPhone());
        orders.setAddress((addressBookInfo.getProvinceName()==null?"":addressBookInfo.getProvinceName())
        +(addressBookInfo.getCityName()==null?"":addressBookInfo.getCityName())
                +(addressBookInfo.getDistrictName()==null?"":addressBookInfo.getDistrictName())
                +(addressBookInfo.getDetail()==null?"":addressBookInfo.getDetail())
        );
        //向订单表插入一条数据
        this.save(orders);
        orderDetailService.saveBatch(orderDetails);
        shoppingCartService.remove(sclqw);
    }

    @Override
    public void again(Orders orders) {
        //获取订单信息
        Orders newOrders = this.getById(orders.getId());
        //获取订单详细信息
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> orderDetailList = orderDetailService.list(lqw);
        long newOrderId= IdWorker.getId();
        //设置新订单信息
        newOrders.setId(newOrderId);
        newOrders.setOrderTime(LocalDateTime.now());
        newOrders.setCheckoutTime(LocalDateTime.now());
        newOrders.setStatus(2);
        //设置新订单详细信息
         orderDetailList = orderDetailList.stream().map(item -> {
            item.setOrderId(newOrderId);
            return item;
        }).collect(Collectors.toList());
        this.save(newOrders);
        orderDetailService.saveBatch(orderDetailList);
    }
}
